<!DOCTYPE>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>一码通网上商城</title>
	<link rel="stylesheet" href="../css/register.css">
	<link rel="stylesheet" href="../css/reset.css">
	<script src="http://beian.80wlkj.com/data/resource/js/jquery.js"></script>
	<script type="text/javascript" src="../js/storage.js"></script>
</head>
<body>
	<div class="register">
		<div class="logo">
			<img src="../imgs/index/logo.png" alt="">
			<p>用户注册</p>
		</div>
		<div class="form">
			<div class="formBar">
				<p>用户名</p>
				<input id="userName" type="text" placeholder="3-15位字符，可由英文、数字组成">
			</div>
			<div class="formBar">
				<p>设置密码</p>
				<input id="userPsd" type="text" placeholder="6-16位字符，可由英文、数字组成">
			</div>
			<div class="formBar">
				<p>确认密码</p>
				<input id="nextPsd" type="text" placeholder=" 请再次输入密码">
			</div>
			<div class="formBar">
				<button id="submitBtn" class="submitBtn">立即注册</button>
			</div>
		</div>
	</div>
</body>
<script type="text/javascript">
	$(function(){
		
		var submitBtn = $('#submitBtn');
		
		submitBtn.click(function(){
			_register();
		});
		$(document).keyup(function(event){
        if(event.keyCode ==13){
         submitBtn.trigger("click");
        }
	})
	function _register(){
		var userName = $('#userName');
		var userPsd  = $('#userPsd');
		var nextPsd = $('#nextPsd');
		var reg = /[a-z,0-9]{6,16}/;
				if (!userName){
				alert('用户名不能为空');
				return false;
			}else if(!userPsd.val()){
				alert('密码不能为空');
				return false;
			}else if(!reg.test(userPsd.val())){
				alert('密码格式不正确');
				return false;
			}else if(!nextPsd.val()){
				alert('确认密码不能为空');
				return false;
			}else if(userPsd.val() != nextPsd.val()){
				alert('俩次密码输入不一致');
				return false;
			}
			$.ajax({
				url:'http://192.168.0.120:8080/ymshoping/register?username='+userName.val()+'&password=' + userPsd.val(),
				dataType:'jsonp',
				jsonp:'callback',//传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名(默认为:callback)
                jsonpCallback:"success_jsonpCallback",//自定义的jsonp回调函数名称，默认为jQuery自动生成的随机函数名
				contentType: 'application/json; charset=utf-8',
				success:function(res){
					console.log(res);
					if (res.code == 0) {
						var tokenJson = {name:'token',value:res.token};
						var usernameJson = {name:'username',value:res.username};
						storage.setStorage(tokenJson);
						storage.setStorage(usernameJson);
						console.log(storage.getStorage('token'),storage.getStorage('username'));
						window.location.href = "./index.html"
					}else if(res.code == 1){
						alert('该用户已存在');
					}
				},
				error:function(res){
					console.log(res.responseText);
				}
			});
	}
})
	
</script>
</html>